////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using RPSDAL;
using RPSDAL.DaoClasses;
using RPSDAL.CollectionClasses;
using RPSDAL.HelperClasses;
using RPSDAL.EntityClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;

public partial class CallFinishedDetails : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadCallFinishedPageFields();
        if (!Page.IsPostBack) 
        {           

            int callId = 0;
            if (Int32.TryParse(Global.DecryptString(Request.QueryString["id"].ToString()), out callId))
            {
                CallEntity eCall = new CallEntity(callId);
                ContactCollection cContact = new ContactCollection();
                IPredicateExpression filter = new PredicateExpression();
                filter.Add(ContactFields.ContactId == eCall.ContactId);
                cContact.GetMulti(filter);

                if (cContact.Count == 1) 
                {
                    lblContactName.Text = cContact[0].Name;
                    lblPhone.Text = cContact[0].Phone;
                    lblMobilePhone.Text = cContact[0].MobilePhone;
                }

                DataTable dtFields = new DataTable();
                ResultsetFields fields = new ResultsetFields(3);

                fields.DefineField(CallFinishedPageFieldFieldIndex.Name, 0, "Name");
                fields.DefineField(CallFinishedValueFieldIndex.FieldValue, 1, "FieldValue");
                fields.DefineField(CallFinishedPageFieldFieldIndex.OrderIndex, 2, "OrderIndex");

                RelationCollection relations = new RelationCollection();
                relations.Add(CallEntity.Relations.CallFinishedValueEntityUsingCallId);
                relations.Add(CallFinishedPageFieldEntity.Relations.CallFinishedValueEntityUsingCallPageFinishedFieldId);

                filter = new PredicateExpression();
                filter.Add(CallFinishedValueFields.CallId == callId);

                TypedListDAO dao = new TypedListDAO();
                SortExpression sortExp = new SortExpression(new SortClause(fields["OrderIndex"], SortOperator.Ascending));
                dao.GetMultiAsDataTable(fields, dtFields, 0, null, filter, relations, false, null, null, 0, 0);

                grdFields.DataSource = dtFields;
                grdFields.DataBind();

            }
        }
        
    }

    protected void Cancel(object sender, EventArgs e)
    {
        Response.Redirect("CallFinishedList.aspx");
    }
   /* protected void MarkCallAsDone(object sender, EventArgs e)
    {
        CallEntity eCall = new CallEntity();
        if (Page.IsPostBack)
        {
            eCall = new CallEntity(Convert.ToInt32(Request.QueryString["id"]));
            CallFinishedValueCollection cCallFinishedValue = new CallFinishedValueCollection();
            IPredicateExpression filter = new PredicateExpression();
            filter.Add(CallFinishedValueFields.CallId == eCall.CallId);
            cCallFinishedValue.DeleteMulti(filter);
        }

        eCall.Done = true;
        eCall.Save();

        // save call finished values fields
        for (int i = 0; i < grdFields.Rows.Count; i++)
        {
            CallFinishedValueEntity eCallFinishedValue = new CallFinishedValueEntity();
            eCallFinishedValue.CallId = eCall.CallId;
            eCallFinishedValue.CallPageFinishedFieldId = Convert.ToInt32(((TextBox)grdFields.Rows[i].Cells[2].FindControl("id")).Text);
            switch (Convert.ToInt32(grdFields.Rows[i].Cells[1].Text))
            {
                case Parameters.TEXTBOX_TYPE:
                case Parameters.DATETIME_TYPE:
                case Parameters.TEXTAREA_TYPE:
                    eCallFinishedValue.FieldValue = ((TextBox)grdFields.Rows[i].Cells[1].Controls[0]).Text;
                    break;
                case Parameters.COMBOBOBOX_TYPE:
                    eCallFinishedValue.FieldValue = ((DropDownList)grdFields.Rows[i].Cells[1].Controls[0]).SelectedValue;
                    break;
                case Parameters.CHECKBOX_TYPE:
                    eCallFinishedValue.FieldValue = ((CheckBox)grdFields.Rows[i].Cells[1].Controls[0]).Checked.ToString();
                    break;
            }

            eCallFinishedValue.Save();
        }

        Response.Redirect("AssignedCallList.aspx");
    }
*/
    private void LoadCallFinishedPageFields() 
    {
        CallFinishedPageFieldCollection cCallFinishedPageFields = new CallFinishedPageFieldCollection();
        cCallFinishedPageFields.GetMulti(null, 0, new SortExpression(new SortClause(CallFinishedPageFieldFields.OrderIndex, SortOperator.Ascending)));

       // grdFields.DataSource = cCallFinishedPageFields;
       // grdFields.DataBind();

       /* for (int i = 0; i < grdFields.Rows.Count; i++)
        {
            try
            {
                switch (Convert.ToInt32(grdFields.Rows[i].Cells[1].Text))
                {
                    case Parameters.TEXTBOX_TYPE:
                        //textbox
                        TextBox txtTextBox = new TextBox();
                        txtTextBox.ID = "txtTextbox";
                        grdFields.Rows[i].Cells[1].Controls.Add(txtTextBox);
                        break;
                    case Parameters.COMBOBOBOX_TYPE:
                        // combobox
                        CallFinishedDropdownFieldValueCollection cComboboxValues = new CallFinishedDropdownFieldValueCollection();
                        IPredicateExpression filter = new PredicateExpression();
                        filter.Add(CallFinishedDropdownFieldValueFields.CallPageFinishedFieldId == Convert.ToInt32(((TextBox)grdFields.Rows[i].Cells[2].FindControl("id")).Text));
                        cComboboxValues.GetMulti(filter);

                        DropDownList cmbComboBox = new DropDownList();
                        cmbComboBox.DataSource = cComboboxValues;
                        cmbComboBox.DataValueField = CallFinishedDropdownFieldValueFields.Value.Name;
                        cmbComboBox.DataTextField = CallFinishedDropdownFieldValueFields.Value.Name;
                        cmbComboBox.DataBind();

                        grdFields.Rows[i].Cells[1].Controls.Add(cmbComboBox);
                        break;
                    case Parameters.CHECKBOX_TYPE:
                        CheckBox ckCehckBox = new CheckBox();
                        grdFields.Rows[i].Cells[1].Controls.Add(ckCehckBox);
                        break;
                    case Parameters.TEXTAREA_TYPE:
                        TextBox txtTextArea = new TextBox();
                        txtTextArea.TextMode = TextBoxMode.MultiLine;
                        grdFields.Rows[i].Cells[1].Controls.Add(txtTextArea);
                        break;
                    case Parameters.DATETIME_TYPE:
                        TextBox txtDateTime = new TextBox();
                        txtDateTime.ID = "txtDateTimeId";
                        grdFields.Rows[i].Cells[1].Controls.Add(txtDateTime);
                        Image imgDateTime = new Image();
                        imgDateTime.ImageUrl = "jscripts/date/calendar.gif";
                        imgDateTime.ID = "dateId";
                        grdFields.Rows[i].Cells[1].Controls.Add(imgDateTime);
                        break;
                }
            }
            catch { }
        }*/
    }
}
